package com.tangyee.home_knowledge.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangyee.R;
import com.tangyee.home_knowledge.bean.KnowledgeContent;
import com.tangyee.home_knowledge.view.AnimatedExpandableListView;

import java.util.List;

/**
 * @author: yu huawen
 * @date: 2019/1/30 11:43
 */
public class KnowledgeSortExpandListAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {
    private LayoutInflater inflater;
    private List<KnowledgeContent> knowledgeContentList;

    public KnowledgeSortExpandListAdapter(Context context, List<KnowledgeContent> knowledgeContentList){
        inflater = LayoutInflater.from(context);
        this.knowledgeContentList = knowledgeContentList;
    }

    public List<KnowledgeContent> getKnowledgeContentList() {
        return knowledgeContentList;
    }

    public void setKnowledgeContentList(List<KnowledgeContent> knowledgeContentList) {
        this.knowledgeContentList = knowledgeContentList;
    }

    @Override
    public View getRealChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View childView = inflater.inflate(R.layout.layout_expand_listview_child_item, null);
        if(getChildrenCount(groupPosition) > 0){
            TextView childTitle = (TextView) childView.findViewById(R.id.child_title);
            childTitle.setText(getKnowledgeContentList().get(groupPosition).getChildListItem().get(childPosition).getCategory());
        }

        return childView;
    }

    @Override
    public int getRealChildrenCount(int groupPosition) {
        return getKnowledgeContentList().get(groupPosition).getChildListItem().size();
    }

    @Override
    public int getGroupCount() {
        return getKnowledgeContentList().size();
    }

    @Override
    public Object getGroup(int i) {
        return getKnowledgeContentList().get(i).getParentItem();
    }

    @Override
    public Object getChild(int i, int i1) {
        return getKnowledgeContentList().get(i).getChildListItem().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    // 分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    @Override
    public boolean hasStableIds() {
        return true;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getGroupView(int i, boolean isExpanded, View view, ViewGroup viewGroup) {
        View parentView = inflater.inflate(R.layout.layout_expand_listview_parent_item, null);
        ImageView parentImg = (ImageView) parentView.findViewById(R.id.parent_img);
        ImageView expandImg = (ImageView) parentView.findViewById(R.id.expand_img);
        TextView parentTitle = (TextView) parentView.findViewById(R.id.parent_title);
        parentTitle.setText(getKnowledgeContentList().get(i).getParentItem().getCategory());
        if (getChildrenCount(i) > 0) { // 一级目录下有二级目录
            if (isExpanded) {
                parentImg.setImageResource(R.drawable.expand_content);
                parentTitle.setTextColor(android.graphics.Color.parseColor("#82C2B3"));
                expandImg.setImageResource(R.drawable.expand_up);
            } else {
                parentImg.setImageResource(R.drawable.not_expand_content);
                parentTitle.setTextColor(android.graphics.Color.parseColor("#4D4E4E"));
                expandImg.setImageResource(R.drawable.expand_down);
            }
        }else { // 一级目录下没有二级目录
            expandImg.setVisibility(View.INVISIBLE);
            parentTitle.setTextColor(android.graphics.Color.parseColor("#4e7457"));
            parentImg.setImageResource(R.drawable.no_child_item);
        }

        return parentView;
    }

    // 指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}
